package Read;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import Definitions.ContentClass;
import Definitions.GraphClass;
import Definitions.NodeClass;
import Global.ClassIdentificationClass;
import Global.ConstantVariable;
import Global.GlobalClass;


public class FileReader 
{
	
	public static void Read(String fileName, GraphClass graph, GlobalClass global) throws NumberFormatException, IOException
	{
		String inputDirectoryPath = ConstantVariable.DefaultDirectoryNamesConstants.ROOT_STATIC_DIRECTORY
		+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderStaticRootDirectory.INPUTS_DIRECTORY;
		ReadAttributesAnsAssignClassForNodes(graph, global, inputDirectoryPath+fileName);
	}
	
	// Return size of nodes to be read
	private static void ReadAttributesAnsAssignClassForNodes(GraphClass graph, GlobalClass global, String fileName) throws NumberFormatException, IOException 
	{
	  	  BufferedReader br = GetBufferedReader(fileName);
	  	  String strLine;
	  	 
	  	  
	  	  for( int nodeIndex = 0 ; (strLine = br.readLine()) != null; nodeIndex++)
	  	  {
	  		  double sum=0;
	  		  double atribute;
	  		  int k;
	  		  
	  		  String[] words = strLine.split(" ");  				// Splitting words with " "
	  		  NodeClass node = new NodeClass(graph);
	  		  
	  		  node.setId(Integer.toString(nodeIndex));									// Setting the node id
	  		  ContentClass content = new ContentClass();
	  		  
	  		  for (k= 0 ; k<words.length -1 ;k++)  				// Filling in the attribute list of the content
	  		  {  
  				  atribute= Float.parseFloat(words[k]) ;
  				  content.getAttributeList().add(atribute) ;
  				  sum +=  atribute*atribute;
	  		  }	    		   
	
	  		  content.setLenght((double)Math.sqrt(sum));
	  		  node.setClassOrder(Integer.parseInt(words[k]));
	  		  node.setOrder(nodeIndex);
	  		  ArrayList<ContentClass> contentList = new ArrayList<ContentClass>(); 
	  		  contentList.add(content);
	  		  node.setContentList(contentList);
	  		  graph.getDataSet().add(node);
	  	  }
	  	  
	  	  for(int i=0; i<10; i++)
	  	  {
	  		  ClassIdentificationClass e = new ClassIdentificationClass();
	          e.setName(Integer.toString(i));
	       	  e.setId(i);
	       	  global.classList.add(e);		        	  
	  	  }
	  	  
	  	  global.classSize = 10;
	}
	
	private static BufferedReader GetBufferedReader(String fileName) throws FileNotFoundException
	{
		FileInputStream fstream = new FileInputStream(fileName);
		DataInputStream contentFile = new DataInputStream(fstream); 					// getting input file	
		return new BufferedReader(new InputStreamReader(contentFile));	// setting the reader class for the input file
	}
}
